package org.tigris.atlas.examples.service;

import org.tigris.atlas.messages.Messages;
import org.tigris.atlas.service.ServiceResponse;
import org.tigris.atlas.examples.enumeration.DayOfWeekEnumeration;
import org.tigris.atlas.examples.enumeration.PrimaryColorValueEnumeration;
import org.tigris.atlas.examples.service.delegate.EntityMaintenanceServiceDelegate;
import org.tigris.atlas.examples.service.delegate.EnumerationExampleServiceDelegate;
import org.tigris.atlas.examples.service.delegate.ServiceDelegateFactory;
import org.tigris.atlas.examples.transfer.EnumerationExample;
import org.tigris.atlas.examples.transfer.EnumerationExamplePK;
import org.tigris.atlas.examples.transfer.TransferObjectFactory;

import java.util.Collection;

import junit.framework.TestCase;

public class TestEnumerations extends TestCase {
	
	EntityMaintenanceServiceDelegate service;
	EnumerationExample parent;
	EnumerationExample parent2;

	protected void setUp() throws Exception {
		super.setUp();
		
		service = ServiceDelegateFactory.createEntityMaintenanceServiceDelegate();
	}
	
	protected void tearDown() throws Exception {		
		service = null;
		
		super.tearDown();		
	}
	
	public void testNamedEnumerationCreation() {
		parent = TransferObjectFactory.createEnumerationExample();
		parent.setSomeNormalTypeField("testNamedEnumerationCreation");
		DayOfWeekEnumeration monday = DayOfWeekEnumeration.getEnum(DayOfWeekEnumeration.MONDAY);		
		parent.setDayOfWeek(monday);
		
		EnumerationExampleServiceResponse response = service.save(parent);		
		verfiyNoErrors(response);
		parent = response.getEnumerationExample();
		EnumerationExamplePK pk = (EnumerationExamplePK)parent.getKey();
		
		//reselect and check for the enumerated value:
		response = service.findByPrimaryKey(pk);
		EnumerationExample reselected = response.getEnumerationExample();
		
		DayOfWeekEnumeration returnedDay = reselected.getDayOfWeek();
		assertEquals("Enumeration was not correctly fetched!", monday, returnedDay);
		
		service.delete( parent.getEnumerationExamplePK() );		
	}
	
	public void testNamedEnumerationUpdate() {
		parent = TransferObjectFactory.createEnumerationExample();
		parent.setSomeNormalTypeField("testNamedEnumerationUpdate");
		DayOfWeekEnumeration monday = DayOfWeekEnumeration.getEnum(DayOfWeekEnumeration.MONDAY);		
		parent.setDayOfWeek(monday);
		
		EnumerationExampleServiceResponse response = service.save(parent);		
		verfiyNoErrors(response);
		parent = response.getEnumerationExample();
		EnumerationExamplePK pk = (EnumerationExamplePK)parent.getKey();
		
		//reselect and check for the enumerated value:
		response = service.findByPrimaryKey(pk);
		EnumerationExample forUpdate = response.getEnumerationExample();
		DayOfWeekEnumeration tuesday = DayOfWeekEnumeration.getEnum(DayOfWeekEnumeration.TUESDAY);
		forUpdate.setDayOfWeek(tuesday);
		response = service.save(forUpdate);
		verfiyNoErrors(response);
		
		response = service.findByPrimaryKey(pk);
		EnumerationExample reselected = response.getEnumerationExample();
		
		DayOfWeekEnumeration returnedDay = reselected.getDayOfWeek();
		assertEquals("Enumeration was not correctly fetched!", tuesday, returnedDay);
		
		service.delete( parent.getEnumerationExamplePK() );
	}	
	
	public void testNamedEnumerationDeletion() {
		parent = TransferObjectFactory.createEnumerationExample();
		parent.setSomeNormalTypeField("testNamedEnumerationDeletion");
		DayOfWeekEnumeration monday = DayOfWeekEnumeration.getEnum(DayOfWeekEnumeration.MONDAY);		
		parent.setDayOfWeek(monday);
		
		EnumerationExampleServiceResponse response = service.save(parent);		
		verfiyNoErrors(response);
		parent = response.getEnumerationExample();
		EnumerationExamplePK pk = (EnumerationExamplePK)parent.getKey();
		
		//reselect and check for the enumerated value:
		response = service.findByPrimaryKey(pk);
		EnumerationExample forUpdate = response.getEnumerationExample();
		forUpdate.setDayOfWeek(null);
		response = service.save(forUpdate);		
		verfiyNoErrors(response);
		forUpdate = response.getEnumerationExample();
		
		response = service.findByPrimaryKey(pk);
		EnumerationExample reselected = response.getEnumerationExample();
		
		DayOfWeekEnumeration returnedDay = reselected.getDayOfWeek();
		assertEquals("Enumeration was not correctly cleared!", null, returnedDay);
		
		service.delete( parent.getEnumerationExamplePK() );		
	}	
	
	public void testNamedEnumerationNull() {
		parent = TransferObjectFactory.createEnumerationExample();
		parent.setSomeNormalTypeField("testNamedEnumerationNull");	
		parent.setDayOfWeek(null);
		
		EnumerationExampleServiceResponse response = service.save(parent);		
		verfiyNoErrors(response);
		parent = response.getEnumerationExample();
		EnumerationExamplePK pk = (EnumerationExamplePK)parent.getKey();
		
		response = service.findByPrimaryKey(pk);
		EnumerationExample reselected = response.getEnumerationExample();
		
		DayOfWeekEnumeration returnedDay = reselected.getDayOfWeek();
		assertEquals("Enumeration was not correctly cleared!", null, returnedDay);

		service.delete( parent.getEnumerationExamplePK() );		
	}
	
	public void testNamedEnumerationQuery() {
		parent = TransferObjectFactory.createEnumerationExample();
		parent.setSomeNormalTypeField("testNamedEnumerationQuery");
		DayOfWeekEnumeration thursday = DayOfWeekEnumeration.getEnum(DayOfWeekEnumeration.THURSDAY);		
		parent.setDayOfWeek(thursday);
				
		EnumerationExampleServiceResponse response = service.save(parent);		
		verfiyNoErrors(response);
		parent = response.getEnumerationExample();
		
		//reselect and check for the enumerated value:
		EnumerationExampleServiceDelegate eeService = ServiceDelegateFactory.createEnumerationExampleServiceDelegate();
		EnumerationExampleCollectionServiceResponse queryResponse = eeService.selectByDay(thursday);
		Collection c = queryResponse.getEnumerationExamples();
		int listSize = c.size();
		assertEquals("Incorrect number of records fetched!", 1, listSize);

		service.delete( parent.getEnumerationExamplePK() );		
	}
	
	public void testValuedEnumerationQuery() {
		parent = TransferObjectFactory.createEnumerationExample();
		parent.setSomeNormalTypeField("testValuedEnumerationQuery");
		PrimaryColorValueEnumeration blue = PrimaryColorValueEnumeration.getEnum(PrimaryColorValueEnumeration.BLUE);		
		parent.setPrimaryColor(blue);
		
		EnumerationExampleServiceResponse response = service.save(parent);		
		verfiyNoErrors(response);
		parent = response.getEnumerationExample();
		
		//reselect and check for the enumerated value:
		EnumerationExampleServiceDelegate eeService = ServiceDelegateFactory.createEnumerationExampleServiceDelegate();
		EnumerationExampleCollectionServiceResponse queryResponse = eeService.selectByColor(blue);
		Collection c = queryResponse.getEnumerationExamples();
		int listSize = c.size();
		assertEquals("Incorrect number of records fetched!", 1, listSize);

		service.delete( parent.getEnumerationExamplePK() );		
	}
	
	public void testMultiEnumerationQuery() {
		parent = TransferObjectFactory.createEnumerationExample();
		parent.setSomeNormalTypeField("testMultiEnumerationQuery1");
		PrimaryColorValueEnumeration blue = PrimaryColorValueEnumeration.getEnum(PrimaryColorValueEnumeration.BLUE);		
		parent.setPrimaryColor(blue);
		DayOfWeekEnumeration thursday = DayOfWeekEnumeration.getEnum(DayOfWeekEnumeration.THURSDAY);		
		parent.setDayOfWeek(thursday);
		parent2 = TransferObjectFactory.createEnumerationExample();
		parent2.setSomeNormalTypeField("testMultiEnumerationQuery");			
		parent2.setPrimaryColor(blue);
		parent2.setDayOfWeek(thursday);
		
		EnumerationExampleServiceResponse response = service.save(parent);	
		parent = response.getEnumerationExample();
		EnumerationExampleServiceResponse response2 = service.save(parent2);	
		parent2 = response2.getEnumerationExample();
		verfiyNoErrors(response);
		verfiyNoErrors(response2);
		
		//reselect and check for the enumerated value:
		EnumerationExampleServiceDelegate eeService = ServiceDelegateFactory.createEnumerationExampleServiceDelegate();
		EnumerationExampleCollectionServiceResponse queryResponse = eeService.selectByColor(blue);
		Collection c = queryResponse.getEnumerationExamples();
		int listSize = c.size();
		assertEquals("Incorrect number of records fetched!", 2, listSize);

		service.delete( parent.getEnumerationExamplePK() );	
		service.delete( parent2.getEnumerationExamplePK() );			
	}		
	
	public void testValuedEnumerationCreation() {
		parent = TransferObjectFactory.createEnumerationExample();
		parent.setSomeNormalTypeField("testValuedEnumerationCreation");
		PrimaryColorValueEnumeration red = PrimaryColorValueEnumeration.getEnum(PrimaryColorValueEnumeration.RED);		
		parent.setPrimaryColor(red);
		
		EnumerationExampleServiceResponse response = service.save(parent);		
		verfiyNoErrors(response);
		parent = response.getEnumerationExample();
		EnumerationExamplePK pk = (EnumerationExamplePK)parent.getKey();
		
		//reselect and check for the enumerated value:
		response = service.findByPrimaryKey(pk);
		EnumerationExample reselected = response.getEnumerationExample();
		
		PrimaryColorValueEnumeration returnedColor = reselected.getPrimaryColor();
		assertEquals("Enumeration was not correctly fetched!", red, returnedColor);

		service.delete( parent.getEnumerationExamplePK() );	
	}
	
	public void testValuedEnumerationUpdate() {
		parent = TransferObjectFactory.createEnumerationExample();
		parent.setSomeNormalTypeField("testValuedEnumerationUpdate");
		PrimaryColorValueEnumeration red = PrimaryColorValueEnumeration.getEnum(PrimaryColorValueEnumeration.RED);		
		parent.setPrimaryColor(red);
		
		EnumerationExampleServiceResponse response = service.save(parent);		
		verfiyNoErrors(response);
		parent = response.getEnumerationExample();
		EnumerationExamplePK pk = (EnumerationExamplePK)parent.getKey();
		
		//reselect and check for the enumerated value:
		response = service.findByPrimaryKey(pk);
		EnumerationExample forUpdate = response.getEnumerationExample();
		PrimaryColorValueEnumeration yellow = PrimaryColorValueEnumeration.getEnum(PrimaryColorValueEnumeration.YELLOW);
		forUpdate.setPrimaryColor(yellow);
		response = service.save(forUpdate);
		forUpdate = response.getEnumerationExample();	
		verfiyNoErrors(response);
		
		response = service.findByPrimaryKey(pk);
		EnumerationExample reselected = response.getEnumerationExample();
		
		PrimaryColorValueEnumeration returnedColor = reselected.getPrimaryColor();
		assertEquals("Enumeration was not correctly fetched!", yellow, returnedColor);

		service.delete( parent.getEnumerationExamplePK() );	
	}
	
	public void testValuedEnumerationDeletion() {
		parent = TransferObjectFactory.createEnumerationExample();
		parent.setSomeNormalTypeField("testValuedEnumerationDeletion");
		PrimaryColorValueEnumeration red = PrimaryColorValueEnumeration.getEnum(PrimaryColorValueEnumeration.RED);		
		parent.setPrimaryColor(red);
		
		EnumerationExampleServiceResponse response = service.save(parent);		
		verfiyNoErrors(response);
		parent = response.getEnumerationExample();
		EnumerationExamplePK pk = (EnumerationExamplePK)parent.getKey();
		
		//reselect and check for the enumerated value:
		response = service.findByPrimaryKey(pk);
		EnumerationExample forUpdate = response.getEnumerationExample();
		forUpdate.setPrimaryColor(null);
		response = service.save(forUpdate);
		forUpdate = response.getEnumerationExample();	
		verfiyNoErrors(response);
		
		response = service.findByPrimaryKey(pk);
		EnumerationExample reselected = response.getEnumerationExample();
		
		PrimaryColorValueEnumeration returnedcolor = reselected.getPrimaryColor();
		assertEquals("Enumeration was not correctly cleared!", null, returnedcolor);

		service.delete( parent.getEnumerationExamplePK() );	
	}	
	
	public void testValuedEnumerationNull() {
		parent = TransferObjectFactory.createEnumerationExample();
		parent.setSomeNormalTypeField("testValuedEnumerationNull");	
		parent.setPrimaryColor(null);
		
		EnumerationExampleServiceResponse response = service.save(parent);		
		verfiyNoErrors(response);
		parent = response.getEnumerationExample();
		EnumerationExamplePK pk = (EnumerationExamplePK)parent.getKey();
		
		response = service.findByPrimaryKey(pk);
		EnumerationExample reselected = response.getEnumerationExample();
		
		PrimaryColorValueEnumeration returnedColor = reselected.getPrimaryColor();
		assertEquals("Enumeration was not correctly cleared!", null, returnedColor);

		service.delete( parent.getEnumerationExamplePK() );	
	}
	
	private void verfiyNoErrors(ServiceResponse parent) {
		assertNotNull("The service response should contain an entity!", parent);
		Messages messages = parent.getMessages();
		int numberOfErrors = (messages != null) ? messages.getErrorMessageCount() : 0;
		assertFalse("No errors should have been returned!", numberOfErrors > 0);	
	}
}
